# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def findMode(self, root):
        if not root:
            return[]
        # T = []
        count = 0
        num = None
        most = 0
        ans = []

        def LRD(root):
            nonlocal count
            nonlocal num
            nonlocal most
            nonlocal ans
            if not root:
                return
            LRD(root.left)
            if num == root.val:
                count = count +1
            else:
                num = root.val
                count = 1
            if count == most:
                ans.append(root.val)
            elif count >most:
                most = count
                ans = [root.val]
            LRD(root.right)

        LRD(root)
        return ans

if __name__ == '__main__':
    p = TreeNode(1)
    p1 = TreeNode(2)
    p2 = TreeNode(2)
    p.right = p1
    p1.left = p2
    A = Solution()
    T = A.findMode(p)
    print(T)